import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    themeColor: undefined, //只有在这里注册之后才能实时渲染界面
    themeFont: undefined
  },
  getters: {
    getByState: state => key => {
      return state[key]
    }
  },
  mutations: {
    // 创建新的属性值
    createState(state, { key, data, save }) {
      if (state[key]) {
        throw new Error(key + ' had be made!')
      }
      if (save) {
        localStorage[key] = JSON.stringify(data)
      }
      state[key] = data
    },
    setState(state, { key, data, save }) {
      if (save) {
        localStorage[key] = JSON.stringify(data)
      }
      // for (let index in data) {
      //   state[key][index] = data[index]
      // }
      state[key] = data
      // Object.assign(state[key], data)
      // Vue.set(state, key, data)
    }
  },
  actions: {},
  modules: {}
})
